﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using Intorise.Common;

namespace TestWindowsFormsApplication.Validator
{
    public class DhlValidator : IValidate
    {
        public Model.ValidateResutlEnum Validate(Model.Country country, string zip, string state, string city, System.Data.DataRow row, Model.FreightInfo info)
        {
            //'已签收 BALGOBI RABIN   BOCA RATON, FL - SPRINGS - USA,2015-6-15
            zip = zip.ToUpper();
            state = state.ToUpper();
            city = city.ToUpper();
            info.ResultText = info.ResultText.ToUpper();
            info.DeliveryCountryCode = info.DeliveryCountryCode.ToUpper();
            info.DeliveryStateCode = info.DeliveryStateCode.ToUpper();
            info.DeliveryCity = info.DeliveryCity.ToUpper();

            if (country == null)
            {
                return ValidateResutlEnum.Incorrect;
            }
            else
            {
                var deliveryCountry = LuluHelper.GetCountry(info.DeliveryCountryCode);
                if (deliveryCountry == null)
                {
                    deliveryCountry = new Country { ID = -1, NameEN = info.DeliveryCountryCode, NameCN = info.DeliveryCountryCode };
                }
                if (deliveryCountry.ID == country.ID || deliveryCountry.NameEN == country.NameEN)//国家相同
                {
                    if (info.ResultText.Contains(zip))//邮编匹配
                    {
                        return ValidateResutlEnum.Correct;
                    }
                    else
                    {
                        var deliveryState = LuluHelper.GetState(info.DeliveryStateCode);
                        if (deliveryState == null)
                        {
                            deliveryState = new State { Code = info.DeliveryStateCode, Name = info.DeliveryStateCode };
                        }
                        if (info.DeliveryCity != city && deliveryState.Name != state)//城市和州都不匹配
                        {
                            return ValidateResutlEnum.Indeterminacy;
                        }
                    }
                }
                else//国家不对
                {
                    return ValidateResutlEnum.Incorrect;
                }
            }
            return ValidateResutlEnum.Correct;
        }
    }
}
